﻿using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;

namespace VS2010.TFSHelperPackage
{
    class WitDocumentHelper
    {

        public static IWorkItemTrackingDocument GetCurrentWitDocument(IServiceProvider serviceProvider)
        {
            Debug.Assert(serviceProvider != null);

            EnvDTE.DTE dte = (EnvDTE.DTE)serviceProvider.GetService(typeof(EnvDTE.DTE));
            if (dte.ActiveDocument == null)
                return null; // No active document

            DocumentService docService = (DocumentService)serviceProvider.GetService(typeof(DocumentService));
            if (docService == null)
                return null; // Possibly VSTS WIT is not installed or instantiated yet.

            IWorkItemTrackingDocument doc = docService.FindDocument(dte.ActiveDocument.FullName, serviceProvider);
            return doc; // Caller must release the document after use
        }

    }
}
